<?php
namespace backend\modules\statistics\models;

use common\libs\Constants;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\models\activeRecords\StatisticsDay;

/**
 * StatisticsSpreadSearch represents the model behind the search form of `backend\models\activeRecords\StatisticsDay`.
 */
class StatisticsSpreadSearch extends StatisticsDay
{
    public $guild_name;
    public $position;
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'reg_num', 'pay_num', 'record_time'], 'integer'],
            [['pay_sum', 'pay_rate', 'arppu', 'next_remains', 'third_remains', 'seven_remains', 'thirty_remains'], 'number'],
            [['game_name','position','guild_name', 'server_name', 'guild_sign', 'spread_key_2', 'date'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = StatisticsDay::find();

        // add conditions that should always apply here
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'reg_num' => $this->reg_num,
            'pay_sum' => $this->pay_sum,
            'pay_num' => $this->pay_num,
            'pay_rate' => $this->pay_rate,
            'arppu' => $this->arppu,
            'next_remains' => $this->next_remains,
            'third_remains' => $this->third_remains,
            'seven_remains' => $this->seven_remains,
            'thirty_remains' => $this->thirty_remains,
            'record_time' => $this->record_time,
        ]);

        if(!empty($this->date)){
            $time = explode(' - ',$this->date);
            $query->andFilterWhere(['between','date'
                ,date('Y-m-d',strtotime($time[0]))
                ,date('Y-m-d',strtotime($time[1]))]);
        }

        $query->andFilterWhere(['like', 'game_name', $this->game_name])
            ->andFilterWhere(['like', 'server_name', $this->server_name])
            ->andFilterWhere(['like', 'guild_sign', $this->guild_sign])
            ->andFilterWhere(['like', 'spread_key_2', $this->spread_key_2])
            ->andFilterWhere(['like', 'date', $this->date]);$auth = Yii::$app->authManager;
        $roleGuildManager = Constants::ROLE_GUILD_MANAGER;
        $roleGuildChannel = Constants::ROLE_GUILD_CHANNEL;
        if(!empty($auth->getRolesByUser(Yii::$app->user->getId())[$roleGuildManager])){
            $query->andFilterWhere(['like', 'statistics_day.guild_sign', Yii::$app->user->getIdentity()->parent]);
            $query->joinWith('user')
                ->andFilterWhere(['like', 'user.position', $this->position]);
        }elseif(!empty($auth->getRolesByUser(Yii::$app->user->getId())[$roleGuildChannel])){
            $query->joinWith('user')
                ->andFilterWhere(['like', 'user.position', Yii::$app->user->getIdentity()->position]);
        }else{
            $query->joinWith('guild')
                ->andFilterWhere(['like', 'guild.guild_name', $this->guild_name]);
            $query->joinWith('user')
                ->andFilterWhere(['like', 'user.position', $this->position]);
        }
        $query->groupBy('spread_key_2');

        return $dataProvider;
    }
}
